package cn.jinjimi.app.managers;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.entity.ZdbProduct;
import cn.jinjimi.app.store.DatabaseManager;
import cn.jinjimi.app.bean.SharePrefKeys;

import java.util.List;

/**
 * Created by lforxeverc on 16/8/17.
 */
public class ZdbCacheManager {
    private static volatile ZdbCacheManager instance = null;

    public static ZdbCacheManager getInstance() {
        if (instance == null) {
            synchronized (ZdbCacheManager.class) {
                if (instance == null) {
                    instance = new ZdbCacheManager();
                }
            }
        }
        return instance;
    }

    /**
     * 保存所有赚多宝产品列表页面数据
     *
     * @param products
     * @param page
     * @param companyCode
     * @param clzCode
     * @param subClzCode
     * @param titles
     */
    public void saveProductAllListCache(List<ZdbProduct> products, int page, String companyCode, String clzCode, String subClzCode, String[] titles) {
        ZdbCacheEntity entity = new ZdbCacheEntity();
        entity.companyCode = companyCode;
        entity.clzCode = clzCode;
        entity.subClzCode = subClzCode;
        entity.titles = titles;
        entity.page = page;
        String data = GsonUtils.toJson(entity);
        PreferenceManager.setData(SharePrefKeys.PREF_ZDB_PAGE_PARAMS, data);
        DatabaseManager.getInstance().getZdbProductAllDao().saveAll(products);
    }

    /**
     * 获取所有赚多宝产品页面缓存数据
     *
     * @return
     */
    public ZdbCacheEntity getProductAllListCache() {
        String params = PreferenceManager.getStringData(SharePrefKeys.PREF_ZDB_PAGE_PARAMS);
        ZdbCacheEntity data = GsonUtils.fromJson(params, ZdbCacheEntity.class);
        if (data == null) {
            data = new ZdbCacheEntity();
        }
        data.mListProduct = DatabaseManager.getInstance().getZdbProductAllDao().loadAll();
        return data;
    }

    /**
     * 保存常用产品列表缓存
     *
     * @param products
     */
    public void saveProductComListCache(List<ZdbProduct> products) {
        DatabaseManager.getInstance().getZdbProductComDao().saveAll(products);
    }

    /**
     * 获取常用产品列表缓存
     *
     * @return
     */
    public List<ZdbProduct> getProductComListCache() {
        return DatabaseManager.getInstance().getZdbProductComDao().loadAll();
    }

    /**
     * 保存是否是演示模式
     *
     * @param showMode
     */
    public void saveShowMode(boolean showMode) {
        PreferenceManager.setData(SharePrefKeys.PREF_ZDB_IS_SHOW_MODE_NEW + AccountManager.getInstance().getAccount().ssoid, showMode);
    }

    /**
     * 获取是否是演示模式
     *
     * @return
     */
    public boolean getShowMode() {
        return PreferenceManager.getBooleanData(SharePrefKeys.PREF_ZDB_IS_SHOW_MODE_NEW + AccountManager.getInstance().getAccount().ssoid, false);
    }

    public class ZdbCacheEntity {
        public int page = 1;
        public String companyCode, clzCode, subClzCode;
        public String[] titles;
        public List<ZdbProduct> mListProduct;
    }


}
